﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    /// <summary>
    /// Extension de MVC para crear links a acciones extendidos
    /// </summary>
    public static class ActionLink
    {
        /// <summary>
        /// Builds Image with an action link
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="imageUrl">Image url</param>
        /// <param name="altText">Text for alt and title attributes</param>
        /// <param name="actionName">Action name</param>
        /// <param name="routeValues">Route values</param>
        /// <param name="htmlAttributes">Hash object with HTML params</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkImage(this HtmlHelper helper, UrlHelper Url, string imageUrl, string altText, string actionName, object routeValues, object htmlAttributes = null)
        {
            var img = helper.Image(imageUrl, altText, htmlAttributes);
            return ActionLinkExtendido(helper, Url, img, actionName, routeValues);
        }

        /// <summary>
        /// Builds Image with an action link
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="imageUrl">Image url</param>
        /// <param name="altText">Text for alt and title attributes</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        /// <param name="htmlAttributes">Hash object with HTML params</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkImage(this HtmlHelper helper, UrlHelper Url, string imageUrl, string altText, string actionName, string controllerName, object htmlAttributes = null)
        {
            var img = helper.Image(imageUrl, altText, htmlAttributes);
            return ActionLinkExtendido(helper, Url, img, actionName, controllerName);
        }

        /// <summary>
        /// Builds Image with an action link
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="imageUrl">Image url</param>
        /// <param name="altText">Text for alt and title attributes</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        /// <param name="htmlAttributes">Hash object with HTML params</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkImage(this HtmlHelper helper, UrlHelper Url, string imageUrl, string altText, string actionName, string controllerName, object routeValues, object htmlAttributes = null)
        {
            var img = helper.Image(imageUrl, altText, htmlAttributes);
            return ActionLinkExtendido(helper, Url, img, actionName, controllerName, routeValues);
        }

        /// <summary>
        /// Construye un action link que contiene un elemento html
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="tag">Elemento html a contener</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkExtendido(this HtmlHelper helper, UrlHelper Url, MvcHtmlString tag, string actionName, object routeValues)
        {
            TagBuilder a = new TagBuilder("a");
            a.MergeAttribute("href", Url.Action(actionName, routeValues));
            a.InnerHtml = tag.ToString();
            return MvcHtmlString.Create(a.ToString(TagRenderMode.Normal));
        }


        /// <summary>
        /// Construye un action link que contiene un elemento html
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="tag">Elemento html a contener</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="controllerName">Nombre del controlador</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkExtendido(this HtmlHelper helper, UrlHelper Url, MvcHtmlString tag, string actionName, string controllerName)
        {
            TagBuilder a = new TagBuilder("a");
            a.MergeAttribute("href", Url.Action(actionName, controllerName));
            a.InnerHtml = tag.ToString();
            return MvcHtmlString.Create(a.ToString(TagRenderMode.Normal));
        }


        /// <summary>
        /// Construye un action link que contiene un elemento html
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="tag">Elemento html a contener</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="controllerName">Nombre del controlador</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkExtendido(this HtmlHelper helper, UrlHelper Url, MvcHtmlString tag, string actionName, string controllerName, object routeValues)
        {
            TagBuilder a = new TagBuilder("a");
            a.MergeAttribute("href", Url.Action(actionName, controllerName, routeValues));
            a.InnerHtml = tag.ToString();
            return MvcHtmlString.Create(a.ToString(TagRenderMode.Normal));
        }
    }
}
